import QtQuick 2.0
import "../program"
import "../components"

Item {
    anchors.fill: parent
    anchors.margins: 15*dp

    // 查看扩增程序 (单拎出来一个页面，不调到扩增程序里了)
    property var gProgramInfo: []        // 扩增程序信息
    property var gProgramStep: []

    property int programType: 0     // 0: 正常的扩增程序  1: 在open表里的扩增程序

    function setProgram(programName){
        if(programName[0] === "."){         // 从AmplOpen里打开
            programType = 1
            var programList = CAmplProgram.getProgramInfo("2")
            gProgramInfo = programList[0]
            gProgramStep = CAmplProgram.getProgramStepInfo(gProgramInfo["Id"], "2")
            pageDetails.setProgramInfo(gProgramInfo)
            pageDetails.setProgramStep(gProgramStep)
        }else{                              // 从Ampl里面打开
            programType = 0
            var programList = CAmplProgram.getProgramInfo("0")
            for(let i = 0; i < programList.length; i ++){
                if(programList[i]["ProgramName"] === programName){
                    var programId = programList[i]["Id"]
                    gProgramInfo = programList[i]
                    gProgramStep = CAmplProgram.getProgramStepInfo(programList[i]["Id"], "0")
                    pageDetails.setProgramInfo(gProgramInfo)
                    pageDetails.setProgramStep(gProgramStep)
                    return
                }
            }
        }


    }

    function saveProgram(){
        gProgramInfo = pageDetails.getProgramInfo()
        gProgramStep = pageDetails.getProgramStep()
        if(programType === 0){
            var res = CAmplProgram.checkedProgramInfo(gProgramInfo, gProgramStep);
            if(res === "校验成功"){
                CAmplProgram.deleteProgramInfo(gProgramInfo["Id"])
                CAmplProgram.saveProgramInfo(gProgramInfo, gProgramStep);
                mainwindow.showToast("保存成功", 0)
            }else{
                mainwindow.showToast("请创建标准扩增程序", 0)
            }
        }else if(programType === 1){
            var res = CAmplProgram.checkedProgramInfo(gProgramInfo, gProgramStep)
            if(res === "校验成功"){
                CAmplProgram.saveProgramInfo_open(gProgramInfo, gProgramStep);
                mainwindow.showToast("保存成功", 0)
            }else{
                mainwindow.showToast("请创建标准扩增程序", 0)
            }

        }
        gIsEditAmplProgram = false      // 返回新建页面

    }
    Item{
        id: experimentProgram
        y: 20*dp
        visible: gIsEditAmplProgram
        anchors.fill: parent
        Rectangle{
            height: 40*dp
            width: parent.width
            color: "transparent"
            visible: curPage !== 0
            Rectangle{
                x: 0
                width: 100*dp
                anchors.verticalCenter: parent.verticalCenter
                color: "transparent"
                Text {
                    text: "查看程序"
                    font.bold: true
                    font.pixelSize: config.fontsize + 4
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
            PushButton{
                id:button_cancel
                x: parent.width-260*dp
                anchors.verticalCenter: parent.verticalCenter
                defaultMode2: true
                title: "取消"
                onReleased: {
                    gIsEditAmplProgram = false
                }
            }
            PushButton{
                id: button_save
                title: "保存"
                x: parent.width-120*dp
                anchors.verticalCenter: parent.verticalCenter
                defaultMode1: true
                onReleased: {
                    saveProgram()
                }
            }
        }
        Rectangle{

            y: 60*dp
            width: parent.width
            height: parent.height - 50*dp

            color: "transparent"
            ProgramList{
                id: pageList
                anchors.fill: parent
                visible: curPage === 0
            }

            ProgramDetails{
                id: pageDetails
                editable: 2
                anchors.fill: parent
                visible: curPage !== 0
            }
        }
    }
}
